/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.Customers.Customer;
import DTO.Customers.Customers;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import util.DBUtil;

/**
 *
 * @author ThanhTai
 */
public class CustomersDAO {

    public Customers checkCustomerLogin(String username, String password) {
        DBUtil util = new DBUtil();
        Connection conn = null;
        Statement stm = null;
        StringBuilder strBuilder = new StringBuilder();
        ResultSet rs = null;
        Customers customers = new Customers();
        try {
            Class.forName(util.getDriverName());
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            conn = DriverManager.getConnection(util.getURL(), util.getUsername(), util.getPassword());
            strBuilder.append("SELECT * FROM Customer");
            if (username != null && password != null) {
                String condition = " WHERE custEmail = '" + username + "' and password = '" + password + "'";
                strBuilder.append(condition);
            }
            stm = conn.createStatement();
            rs = stm.executeQuery(strBuilder.toString());
            if (rs.next()) {
                Customer customer = new Customer(rs.getString(1), rs.getString(2), rs.getString(3),
                        rs.getString(4), rs.getString(5));
                customers.addCustomer(customer);

            }
            return customers;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    public boolean isEmailExisted(String email) {
        if (email != null && !email.equals("")) {
            DBUtil util = new DBUtil();
            Connection conn = null;
            Statement stm = null;
            StringBuilder strBuilder = new StringBuilder();
            ResultSet rs = null;
            try {
                Class.forName(util.getDriverName());
                DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
                conn = DriverManager.getConnection(util.getURL(), util.getUsername(), util.getPassword());
                strBuilder.append("SELECT * FROM Customer");
                String condition = " WHERE custEmail = " + "'" + email + "'";
                strBuilder.append(condition);
                stm = conn.createStatement();
                rs = stm.executeQuery(strBuilder.toString());
                if (rs.next()) {
                    return true;
                } else {
                    return false;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (rs != null) {
                        rs.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    return false;
                }
                try {
                    if (stm != null) {
                        stm.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    return false;
                }
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            return false;
        } else {
            return true;
        }
    }

    public boolean InsertCustomer(Customer customer) {
        DBUtil util = new DBUtil();
        Connection conn = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            Class.forName(util.getDriverName());
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            conn = DriverManager.getConnection(util.getURL(), util.getUsername(), util.getPassword());
            String sql = "";
            if (conn != null) {
                sql = "Insert into Customer(custEmail, password, custName, custPhone, custAddress) values (?, ?, ?, ?, ?)";
                stm = conn.prepareStatement(sql);
                stm.setString(1, customer.getCustEmail());
                stm.setString(2, customer.getCustPassword());
                stm.setString(3, customer.getCustName());
                stm.setString(4, customer.getCustPhone());
                stm.setString(5, customer.getCustAddress());
                Integer result = stm.executeUpdate();
                if (result>0) {
                    return true;
                } else {
                    return false;
                }
            }else{
                return false;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
    }
}
